1   /*
2    * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.font;
27  
28  import sun.java2d.Disposer;
29  import sun.java2d.DisposerRecord;
30  
31  /*
32   * This keeps track of data that needs to be cleaned up once a
33   * strike is freed.
34   * a) The native memory that is the glyph image cache.
35   * b) removing the "desc" key from the strike's map.
36   * This is safe to do because this disposer is invoked only when the
37   * reference object has been cleared, which means the value indexed by
38   * this key is just an empty reference object.
39   * It is possible that a new FontStrike has been created that would
40   * be referenced by the same (equals) key. If it is placed in the map
41   * before this disposer is executed, then we do not want to remove that
42   * object. We should only remove an object where the value is null.
43   * So we first verify that the key still points to a cleared reference.
44   * Updates to the map thus need to be synchronized.
45   *
46   * A WeakHashmap will automatically clean up, but we might maintain a
47   * reference to the "desc" key in the FontStrike (value) which would
48   * prevent the keys from being discarded. And since the strike is the only
49   * place is likely we would maintain such a strong reference, then the map
50   * entries would be removed much more promptly than we need.
51   */
52  
53  class FontStrikeDisposer
54      implements DisposerRecord, Disposer.PollDisposable {
55  
56      Font2D font2D;
57      FontStrikeDesc desc;
58      long[] longGlyphImages;
59      int [] intGlyphImages;
60      int [][] segIntGlyphImages;
61      long[][] segLongGlyphImages;
62      long pScalerContext = 0L;
63      boolean disposed = false;
64      boolean comp = false;
65  
66      public FontStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
67                                long pContext, int[] images) {
68          this.font2D = font2D;
69          this.desc = desc;
70          this.pScalerContext = pContext;
71          this.intGlyphImages = images;
72      }
73  
74      public FontStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
75                                long pContext, long[] images) {
76          this.font2D = font2D;
77          this.desc = desc;
78          this.pScalerContext = pContext;
79          this.longGlyphImages = images;
80      }
81  
82      public FontStrikeDisposer(Font2D font2D, FontStrikeDesc desc,
83                                long pContext) {
84          this.font2D = font2D;
85          this.desc = desc;
86          this.pScalerContext = pContext;
87      }
88  
89      public FontStrikeDisposer(Font2D font2D, FontStrikeDesc desc) {
90          this.font2D = font2D;
91          this.desc = desc;
92          this.comp = true;
93      }
94  
95      public synchronized void dispose() {
96          if (!disposed) {
97              font2D.removeFromCache(desc);
98              StrikeCache.disposeStrike(this);
99              disposed = true;
100         }
101     }
102 }